package me.wangxuan.pat;

import java.util.logging.Logger;

import javax.persistence.EntityManagerFactory;
//import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import com.formdev.flatlaf.FlatDarkLaf;

import me.wangxuan.pat.controller.MainController;
import me.wangxuan.pat.repository.AccountRepository;
import me.wangxuan.pat.repository.ApiRepository;
import me.wangxuan.pat.repository.ProjectRepository;
import me.wangxuan.pat.repository.RepositoryProxy;
import me.wangxuan.pat.view.MainView;

public class PayApiToolMain {
	private static final Logger logger = Logger.getLogger("global");
	public static void main(String[] args) {
		try {
			LoggingConfig.init();
			System.out.println(UIManager.getSystemLookAndFeelClassName());
//		    UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
//		    UIManager.setLookAndFeel(FlatLightLaf.class.getName());
		    UIManager.setLookAndFeel(FlatDarkLaf.class.getName());
		} catch (Exception e) {
		    e.printStackTrace();
		}
		ProjectRepository projectRepo = RepositoryProxy.createProxy(ProjectRepository.class);
//		List<Project> list = projectRepo.findAll();
//		logger.info("projects:"+list.get(0));
		AccountRepository accountRepo = RepositoryProxy.createProxy(AccountRepository.class);
//		logger.info("accounts:"+accountRepo.findAll().get(0).getProject().getProjectId());
		ApiRepository apiRepo = RepositoryProxy.createProxy(ApiRepository.class);
		
		// 注册关闭钩子
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        	EntityManagerFactory emf = JpaConfig.getEntityManagerFactory();
        	logger.info("ShutdownHook:"+emf);
            if (emf != null && emf.isOpen()) {
                emf.close();
            }
        }));
        
		SwingUtilities.invokeLater(() -> {
//            JFrame frame = new JFrame("SwingApp");
//            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//            frame.setSize(400, 300);
//            frame.setVisible(true);
			// new CustomLayout();
			MainView view = new MainView();
			MainController ctl = new MainController(view,projectRepo,accountRepo,apiRepo);
			ctl.showView();
        });
	}

}
